<?php
namespace Admin\Controller;
use Think\Controller;
class LibraryController extends BaseControlle {

    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $this->display('index');
    }

    //新建题库的前端UI
    public function NewLibraryUI(){

        $grade               = M("grade");
        $subject_list        = M("subject_list");
        $ret_grade        = $grade->order('number')->select();
        $ret_subject_list = $subject_list->order('subject_order ASC')->select();

        $this->assign('grade',$ret_grade);
        $this->assign('ret_subject_list',$ret_subject_list);
        $this->display();
    }

    /**
     * 添加题库
     */
    public function NewLibrary(){
        if(IS_POST){
            $title      = I("post.title");
            $subject    = I("post.subject");
            $grade      = I("post.grade");
            $status     = I("post.status",0,'intval');
            $add_uid    = 1;
            $add_date = date(time());
            if(!$title || !$subject || !$grade){
                $this->ajaxReturn(array('code'=>500,'msg'=>'请按要求提交数据'));
            }

            $db = array(
                'title'=>$title,
                'subject'=> $subject,
                'grade'=>$grade,
                'status'=>$status,
                'add_uid'=>$add_uid,
                'add_date'=>$add_date
            );

            $examination = M('examination');
            $ret = $examination->data($db)->add();

            if($ret){
                $this->ajaxReturn(array('code'=>200));
            }else{
                $this->ajaxReturn(array('code'=>501,'msg'=>'数据添加失败！'));
            }

        }else{
            $this->ajaxReturn(array('code'=>502,'msg'=>'提交非法数据！'));
        }
    }

    /**
     * 更新题库题目信息
     */
    public function UpdateLibrary(){
        if(IS_POST){
            $examinationid = I("post.examinationid");
            $title          = I("post.title");
            $subject        = I("post.subject");
            $grade          = I("post.grade");
            $status     = I("post.status",0,'intval');
            $add_uid        = 1;
            $add_date = date(time());

            if(!$examinationid || !$title || !$subject || !$grade){
                $this->ajaxReturn(array('code'=>500,'msg'=>'请按要求提交数据'));
            }

            $db = array(
                'title'=>$title,
                'subject'=> $subject,
                'grade'=>$grade,
                'status'=>$status,
                'add_uid'=>$add_uid,
                'add_date'=>$add_date
            );

            $examination = M('examination');
            $ret = $examination->where("examinationid='$examinationid'")->save($db);

            if($ret){
                $this->ajaxReturn(array('code'=>200));
            }else{
                $this->ajaxReturn(array('code'=>501,'msg'=>'数据修改失败！'));
            }

        }else{
            $this->ajaxReturn(array('code'=>502,'msg'=>'提交非法数据！'));
        }
    }
    //删除题库题目信息
    public function DeleteLibrary(){
        $LibraryId = I('post.libraryid',0,'intval');
        $examination = M('examination');
        $ret = $examination->where("examinationid='$LibraryId'")->delete();
        if($ret){
            $this->ajaxReturn(array('code'=>200));
        }else{
            $this->ajaxReturn(array('code'=>5011,'msg'=>'删除失败！'));
        }
    }

    //题库管理
    public function Manage(){
        $examination = M('examination');

        $DB_PREFIX = C('DB_PREFIX');

        $page   =      I('get.page');
        $count  =      $examination->count();
        $page   = DBPage($count,30,$page);

        $grade  = M("grade");
        $ret_grade = $grade->order('number')->select();

        $this->assign('grade',$ret_grade);
        $examination_list = $examination
            ->join("__GRADE__ ON __GRADE__.id=__EXAMINATION__.grade",'LEFT')
            ->join("__SUBJECT_LIST__ ON __SUBJECT_LIST__.id=__EXAMINATION__.subject",'LEFT')
            ->limit(30)
            ->page($page['page'])
            ->order('examinationid asc')
            ->field("{$DB_PREFIX}examination.*,{$DB_PREFIX}grade.name as gradename,{$DB_PREFIX}subject_list.subject_name")
            ->order('examinationid DESC')
            ->select();
        $this->assign('examination_list',$examination_list);
        $this->assign('count',$page['countPage']);
        $this->assign('page',$page['page']);
        $this->display('Manage');
    }

    //编辑题库
    public function EditLibrary(){
        $LibraryId = I('get.libraryid');
        $examination = M('examination');
        $DB_PREFIX = C('DB_PREFIX');

        $grade               = M("grade");
        $ret_grade = $grade->order('number')->select();

        $subject_list = M('subject_list');
        $ret_subject_list = $subject_list->order('subject_order ASC')->select();

        $examination_list = $examination
            ->join("__GRADE__ ON __GRADE__.id=__EXAMINATION__.grade",'LEFT')
            ->where("examinationid='$LibraryId'")
            ->field("{$DB_PREFIX}examination.*,{$DB_PREFIX}grade.name as gradename")
            ->find();
        $this->assign('grade',$ret_grade);
        $this->assign('ret_subject_list',$ret_subject_list);
        $this->assign('examination_list',$examination_list);
        $this->display();
    }
}